using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ActorAnimator : MonoBehaviour {

    public List<Transform> m_PartsToPaint = new List<Transform>();
    Actor m_Actor;
    Animation anim;
    //const float c_KnifeAnimDuration
    bool animatingKnifeAttack = false;
    void Awake()
    {
        anim = this.GetComponent<Animation>();
    }
    public void Initialize(Actor actor,int index)
    {
        m_Actor = actor;
        PaintParts(index);
    }
	void Update () 
    {
        if (m_Actor.IsAlive())
        {
            HandleMoveAnimations();
        }
	}
    void HandleMoveAnimations()
    {
        //Si esta haciendo animacion de cuchillo no hago las demas hasta que termine.
        if (animatingKnifeAttack)
        {
            if (!anim.isPlaying)
            {
                animatingKnifeAttack = false;
            }
            else return;
        }
        if (m_Actor.m_Crouching)
        {
            if (m_Actor.m_Moving)
            {
                anim.Play("soldierCrouchRun");
            }
            else
            {
                anim.Play("soldierCrouch");
            }
        }
        else
        {
            if (m_Actor.m_Moving)
            {
                if (m_Actor.m_Sprinting)
                {
                    anim.Play("soldierSprint");
                }
                else
                {
                    anim.Play("soldierWalk");
                }
            }
            else
            {
                anim.Play("soldierIdle");
            }
        }
    }
    public void PlayDeadAnimation()
    {
        anim.Play("soldierDieBack");
    }
    public void PlayKnifeAnimation()
    {
        animatingKnifeAttack = true;
        anim.Play("soldierHitFront");
    }
    void PaintParts(int index)
    {
        Color colorToUse;
        switch (index)
        {
            case -2://enemigo de rank soldado
                colorToUse = new Color(1, 0.0f, 0.0f);
                break;
            case -1:
                colorToUse = new Color(0.3f, 0.1f, 0.1f);
                break;
            case 0:
                colorToUse = new Color(0, 0.2f, 1);
                break;
            case 1:
                colorToUse = new Color(0, 1, 0.2f);
            break;
            case 2:
                colorToUse = new Color(1, 1, 0);
            break;
            case 3:
                colorToUse = new Color(1, 1, 1);
            break;
            default:
                colorToUse = new Color(1, 1, 1);
            break;
        }
        foreach (Transform t in m_PartsToPaint)
        {
            //t.renderer.material = new Material(t.renderer.material);
            //t.renderer.material.CopyPropertiesFromMaterial(t.renderer.material);
            t.renderer.material.color = colorToUse;
        }
    }
}
